﻿using JWT.Exceptions;
using JWT;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using SmartWMS.API.Controllers;
using SmartWMS.Common;
using SmartWMS.Common.Extensions;
using SmartWMS.Common.Jwt;
using SmartWMS.Common.ResponseOutput;
using SmartWMS.Domain.SystemSet;
using SmartWMS.Server.SystemSets.StaffServices;
using SmartWMS.Server.SystemSets.StaffServices.Dto;

namespace SmartWMS.API.Controllers.SystemSet
{
    /// <summary>
    /// 人员控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "XTong")]
    public class StaffController : TransientController
    {
        private readonly IStaffService _staffService;

        public StaffController(IStaffService staffService)
        {
            _staffService = staffService;
        }

        /// <summary>
        /// 人员列表
        /// </summary>
        /// <param name="staffName"></param>
        /// <returns></returns>
        [HttpGet]
        public Response<List<StaffInput>> GetStaffListAsync()
        {
            var result = _staffService.GetStaffListAsync();

            return result.Result;
        }
        /// <summary>
        /// 人员分配角色
        /// </summary>
        /// <param name="staffId"></param>
        /// <param name="roleIds"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> AddStaffRoleAsync(StaffRoleInput input)
        {
            var result = _staffService.AddStaffRoleAsync(input);
            return result.Result;
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost, AllowAnonymous]
        public IActionResult Login(StaffInput input)
        {
            var result = _staffService.LoginAsync(input);
            var accountToken = "";
            if (result.Result != null)
            {
                accountToken = GetAccountToken(result.Result.Data);
            }
            return Ok(new
            {
                success = true,
                accountToken,
                data = result.Result.Data
            });
        }
        /// <summary>
        /// 获取token令牌
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpGet("GetAccountToken")]
        public string GetAccountToken(StaffInput user)
        {
            var staff = user.MapTo<Staff>();
            string jwtToken = JwtHelper.CreateClaims(staff);
            return jwtToken;
        }
        /// <summary>
        /// 获取人员角色关联表数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public List<int> GetRoleByIdAsync(int id)
        {
            var result = _staffService.GetRoleByIdAsync(id);
            return result;
        }


    }
}
